package com.mr.cai.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mr.cai.main.R;

/**
 * Created by Administrator on 16-2-28.
 */
public class ClassifyPopAdapter extends RecyclerView.Adapter<ClassifyPopAdapter.MyViewHolder> {
    private Context mContext;
    private String[] test={
            "默认",
            "销量",
            "价格",
            "评价高低"
    };
    public ClassifyPopAdapter(Context mContext) {
        this.mContext = mContext;
    }

    private int mPosition;
    private OnItemClickLitener onItemClickListener;


    public void setOnItemClickListener(OnItemClickLitener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }


    public int getmPosition() {
        return mPosition;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View item=LayoutInflater.from(
                mContext).inflate(R.layout.classify_pop_adapter_layout, parent,
                false);
        final MyViewHolder holder = new MyViewHolder(item);
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos=holder.getPosition();
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(view,pos);
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(mPosition==position){
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.color_main_green));
            holder.typeFlag.setVisibility(View.VISIBLE);
        }else{
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.color_black));
            holder.tv.setSelected(false);
            holder.typeFlag.setVisibility(View.GONE);
        }
        holder.tv.setText(test[position]);
    }

    @Override
    public int getItemCount() {
        return test.length;
    }

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        ImageView typeFlag;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv=(TextView)itemView.findViewById(R.id.typeText);
            typeFlag=(ImageView)itemView.findViewById(R.id.selFlag);
        }
    }
}
